// Fill out your copyright notice in the Description page of Project Settings.

#include "Isalong2.h"
#include "DynamicTreePartBase.h"
#include "DynamicTreeBase.h"




ADynamicTreeBase::ADynamicTreeBase()
{
	_part0 = CreateDefaultSubobject<UDynamicTreePartBase>(TEXT("Part0"));
	_part1 = CreateDefaultSubobject<UDynamicTreePartBase>(TEXT("Part1"));
	_part2 = CreateDefaultSubobject<UDynamicTreePartBase>(TEXT("Part2"));
	_part3 = CreateDefaultSubobject<UDynamicTreePartBase>(TEXT("Part3"));
	_part4 = CreateDefaultSubobject<UDynamicTreePartBase>(TEXT("Part4"));
	_part5 = CreateDefaultSubobject<UDynamicTreePartBase>(TEXT("Part5"));
	_part6 = CreateDefaultSubobject<UDynamicTreePartBase>(TEXT("Part6"));
	_part7 = CreateDefaultSubobject<UDynamicTreePartBase>(TEXT("Part7"));
	_part8 = CreateDefaultSubobject<UDynamicTreePartBase>(TEXT("Part8"));
	_part9 = CreateDefaultSubobject<UDynamicTreePartBase>(TEXT("Part9"));
	_part10 = CreateDefaultSubobject<UDynamicTreePartBase>(TEXT("Part10"));
	_part11 = CreateDefaultSubobject<UDynamicTreePartBase>(TEXT("Part11"));

	_part0->SetPartOwner(this);
	_part1->SetPartOwner(this);
	_part2->SetPartOwner(this);
	_part3->SetPartOwner(this);
	_part4->SetPartOwner(this);
	_part5->SetPartOwner(this);
	_part6->SetPartOwner(this);
	_part7->SetPartOwner(this);
	_part8->SetPartOwner(this);
	_part9->SetPartOwner(this);
	_part10->SetPartOwner(this);
	_part11->SetPartOwner(this);

	_part0->AttachTo(RootComponent);
	_part0->SetRelativeLocation(FVector(0, 0, _part0->GetHeight() / 2));
	_part0->SetUpPart(_part1);
	_part1->SetUpPart(_part2);
	_part2->SetUpPart(_part3);
	_part3->SetUpPart(_part4);
	_part4->SetUpPart(_part5);
	_part5->SetUpPart(_part6);
	_part6->SetUpPart(_part7);
	_part7->SetUpPart(_part8);
	_part8->SetUpPart(_part9);
	_part9->SetUpPart(_part10);
	_part10->SetUpPart(_part11);

	_part0->SetStaticMesh(LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));
	_part1->SetStaticMesh(LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));
	_part2->SetStaticMesh(LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));
	_part3->SetStaticMesh(LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));
	_part4->SetStaticMesh(LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));
	_part5->SetStaticMesh(LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));
	_part6->SetStaticMesh(LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));
	_part7->SetStaticMesh(LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));
	_part8->SetStaticMesh(LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));
	_part9->SetStaticMesh(LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));
	_part10->SetStaticMesh(LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));
	_part11->SetStaticMesh(LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));

	_part0->SnapUp();
}

void ADynamicTreeBase::OnHurt(AActor* shotter, FVector impactNormal, FVector hitLocation, int hurtType, float hurtValue, UPrimitiveComponent* hitComponent)
{
	auto target = Cast<UDynamicTreePartBase>(hitComponent);
	if (target!=nullptr)
	{
		target->NodeHurtCheck(hitLocation, hurtType, hurtValue);
	}
}

void ADynamicTreeBase::OnPartBreak(UDynamicTreePartBase* rootPart)
{
	rootPart->DetachFromParent(true);
	rootPart->AttachTo(_mesh,NAME_None,EAttachLocation::KeepWorldPosition);
}
